package com.study.map;

import java.util.Arrays;
import java.util.HashMap;

public class HashMapTest {

    private HashMap map = new HashMap();

    public HashMapTest() {
        Thread t1 = new Thread() {
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    map.put(new Integer(i), i);
                }
                System.out.println("t1 over");
            }
        };

        Thread t2 = new Thread() {
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    map.put(new Integer(i), i);
                }

                System.out.println("t2 over");
            }
        };

        Thread t3 = new Thread() {
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    map.put(new Integer(i), i);
                }

                System.out.println("t3 over");
            }
        };

        Thread t4 = new Thread() {
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    map.put(new Integer(i), i);
                }

                System.out.println("t4 over");
            }
        };

        Thread t5 = new Thread() {
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    map.put(new Integer(i), i);
                }

                System.out.println("t5 over");
            }
        };

        Thread t6 = new Thread() {
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    map.get(new Integer(i));
                }

                System.out.println("t6 over");
            }
        };

        Thread t7 = new Thread() {
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    map.get(new Integer(i));
                }

                System.out.println("t7 over");
            }
        };

        Thread t8 = new Thread() {
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    map.get(new Integer(i));
                }

                System.out.println("t8 over");
            }
        };

        Thread t9 = new Thread() {
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    map.get(new Integer(i));
                }

                System.out.println("t9 over");
            }
        };

        Thread t10 = new Thread() {
            public void run() {
                for (int i = 0; i < 50000; i++) {
                    map.get(new Integer(i));
                }

                System.out.println("t10 over");
            }
        };

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();

        t6.start();
        t7.start();
        t8.start();
        t9.start();
        t10.start();
    }
    //不断的往一个非线程安全的HashMap中put内容/get内容，put的内容很简单，key和value都是从0自增的整数.
    public static void main(String[] args) {
        //new HashMapTest();
        
        HashMap<String, String> hashMap = new HashMap<String,String>();
        hashMap.put(null, null);
        System.out.println(hashMap.size()+","+hashMap.get(null));
        
        int[] arr = { 10,20,5,3,7,4,9 };
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        
        System.out.println(findMedianInArray(arr,3,0,6));
        sort(arr);
        
        
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        
        
    }
    
    
    //findMedianInArray(int [] arr, int idx, int begin, int end)
    public static int findMedianInArray(int [] arr, int idx, int begin, int end){
    	//int median = 0;
    	int length = arr.length;
    	int min = length /2 -1;
    	
    	int x = begin;
    	int tmp = arr[begin];
    	
    	for(int i = begin ; i <= end ; i ++){
    		if(arr[i] < tmp){
    			arr[x] = arr[i];
    			x++;
    		}
    	}
    	arr[x] = tmp;
    	
    	if(x == idx){
    		return tmp;
    	}else if(x < idx -1){
    		return findMedianInArray(arr, min - idx , x+1, end);
    	}else {
    		return findMedianInArray(arr, idx , 0, x);
    	}
    }
    public static void sort(int []arr){
    	int length = arr.length;
    	for(int i = 0; i<length; i++){
    		for(int j = i; j<length; j++){
    			if(arr[i] > arr[j]){
    				int tmp = arr[i];
    				arr[i] = arr[j];
    				arr[j] = tmp;
    			}
    		}
    		
    	}
    }
}